package shotgun.my.sweetutil.http.impl;

import okhttp3.*;
import okhttp3.internal.Util;
import okhttp3.internal.http.BridgeInterceptor;
import org.apache.commons.lang3.StringUtils;
import shotgun.my.sweetutil.http.HttpClientUtil;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author wulm
 **/
public class OkHttpClientUtilImpl implements HttpClientUtil {

    public static final OkHttpClient CLIENT_DEFAULT = new OkHttpClient.Builder()
            .addInterceptor(new BridgeInterceptor(CookieJar.NO_COOKIES))
            .connectTimeout(HttpConfig.HTTP_CONFIG_DEFAULT.getConnectTimeout(), TimeUnit.SECONDS)
            .readTimeout(HttpConfig.HTTP_CONFIG_DEFAULT.getReadTimeout(), TimeUnit.SECONDS).build();

    @Override
    public String execute(String method, String url, Map<String, String> headers, String body,
            HttpConfig httpConfig) {
        if (StringUtils.isEmpty(method)) {
            throw new IllegalArgumentException("method不能为空!");
        }
        if (StringUtils.isEmpty(url)) {
            throw new IllegalArgumentException("url不能为空!");
        }

        try {
            method = method.toUpperCase();

            RequestBody requestBody;

            if (GET.equals(method)) {

                if (headers == null) {
                    headers = Collections.emptyMap();
                }
                //GET、HEAD请求，requestBody必须为null
                requestBody = null;

            } else {
                //post和其他处理

                if (headers == null) {
                    //设置默认Content-Type为application/x-www-form-urlencoded
                    headers = new HashMap<>(1);
                    headers.put(HEADER_KEY_CONTENT_TYPE, HEADER_VAL_CONTENT_TYPE_FORM);
                } else if (!headers.containsKey(HEADER_KEY_CONTENT_TYPE)) {
                    //没有Content-Type，设置默认Content-Type为application/x-www-form-urlencoded
                    headers.put(HEADER_KEY_CONTENT_TYPE, HEADER_VAL_CONTENT_TYPE_FORM);
                }
                //除GET、HEAD请求外，requestBody不能为null
                requestBody = StringUtils.isEmpty(body) ? Util.EMPTY_REQUEST : RequestBody
                        .create(MediaType.parse(headers.get(HEADER_KEY_CONTENT_TYPE)), body);
            }
            //headers和requestBody处理完毕
            //进行公共处理

            //去除Accept-Encoding、Range头部信息，使用okhttp3处理gzip，否则无法自动解析gzip压缩内容出现乱码
            headers.remove(HEADER_KEY_ACCEPT_ENCODING);
            headers.remove(HEADER_KEY_RANGE);

            //创建okHttpClient对象
            OkHttpClient okHttpClient;
            if (httpConfig == null || httpConfig == HttpConfig.HTTP_CONFIG_DEFAULT) {
                okHttpClient = CLIENT_DEFAULT;
            } else {
                okHttpClient = new OkHttpClient.Builder()
                        .addInterceptor(new BridgeInterceptor(CookieJar.NO_COOKIES))
                        .connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.SECONDS)
                        .readTimeout(httpConfig.getReadTimeout(), TimeUnit.SECONDS).build();
            }


            // 构造Request
            Request request = new Request.Builder().url(url)
                    .headers(headers.isEmpty() ? new Headers.Builder().build() : Headers.of(headers))
                    .method(method, requestBody).build();

            Call call = okHttpClient.newCall(request);

            //发起请求
            Response response = call.execute();

            ResponseBody responseBody = response.body();

            return responseBody == null ? "" : responseBody.string();
//            if (response.isSuccessful()) {
//            }
        } catch (IOException e) {
            throw new IllegalArgumentException("http error!", e);
        }
    }

}
